import requests
import pymysql
import json
import time
import random
headers={
'Host':'apis.map.qq.com',
'User-agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.63 Safari/537.36 Qiyu/2.1.1.1',
'Cookie':'pgv_pvi=7304519680; RK=xNIR+nlwPN; tvfe_boss_uuid=360b2156a71551bf; pgv_pvid=9804905796; ptcz=32fe644e71693e5eef2d81260c9756b2c9601865be79481b4f8e6e456dafdcfa; pt2gguin=o2701305387'
}
# 1.查询数据库，获取城市名称
conn=pymysql.connect(host='127.0.0.1',user='root',password='FanTan879',db='zw_spider',charset='utf8')
cursor=conn.cursor()
cursor.execute('select dd from zp_item where province='' group by dd')
result=cursor.fetchall()
for index,city in enumerate(result):
    time.sleep(random.randint(1,5))
        # 2.拼接查询链接
    if city[0]:
        url='http://apis.map.qq.com/jsapi?qt=poi&wd={}&pn=0&rn=10&rich_source=qipao&rich=web&nj=0&c=1&key=FBOBZ-VODWU-C7SVF-B2BDI-UK3JE-YBFUS&pf=jsapi&ref=jsapi'.format(city[0])
        try:
            # 执行URL查询
            response=requests.get(url,headers=headers).text
            json_text=response.strip('qq.maps._svcb3.search_service_0 && qq.maps._svcb3.search_service_0()')
            #print(json_text)
            json_dict=json.loads(json_text)
            pointx=json_dict['detail']['city'].get('pointx','')
            pointy=json_dict['detail']['city'].get('pointy','')
            cname=json_dict['detail'].get('parent',{}).get('cname','')
            if cname=='中国':
                cname=city[0]
            elif cname=='':
                url='http://api.map.baidu.com/?qt=s&c=289&wd={}&rn=10&ie=utf-8&oue=1&fromproduct=jsapi&res=api&callback=BMap._rd._cbk77164&ak=E4805d16520de693a3fe707cdc962045'.format(city[0]+'市')
                response = requests.get(url).text
                json_text = response.strip('/**/BMap._rd._cbk77164 && BMap._rd._cbk77164()')
                json_dict = json.loads(json_text)
                cname=json_dict['current_city']['up_province_name']

            cname=cname.strip('壮族回族维吾尔市省自治区')
            if cname=='上海':
                cname=city[0]
            # 更新数据库记录
            update_sql='update zp_item set pointx=%s,pointy=%s,province=%s where dd_name=%s and province=""'
            update_parm=(pointx,pointy,cname,city[0])
            print(update_parm)
            cursor.execute(update_sql,update_parm)
            conn.commit()
        except Exception as e:
            print(e)
            print(url)
            #print(requests.get(url).text)
